const serverUrl: string = 'ws://localhost:3000'

const useWebsocket = (messageHandle: any) => {
  const ws = new WebSocket(serverUrl)

  const init = () => {
    bindEvent()
  }

  const openHandle = (e: Object) => {
    console.log('openHandle', e)
  }

  const closeHandle = (e: Object) => {
    console.log('closeHandle', e)
  }

  const errorHandle = (e: Object) => {
    console.log('errorHandle', e)
  }

  // 绑定事件处理函数
  const bindEvent = (): void => {
    ws.addEventListener('open', openHandle, false)
    ws.addEventListener('close', closeHandle, false)
    ws.addEventListener('error', errorHandle, false)
    ws.addEventListener('message', messageHandle, false)
  }

  init()

  return ws
}


export default useWebsocket

